<?php
error_reporting(E_ALL);
ini_set("display_errors","On");
ini_set("SMTP","smtp.qq.com");
//ini_set("include_path",".;C:\wamp\bin\php\php5.2.6\PEAR");
ini_set("xdebug.auto_trace","on");
if (empty($_SESSION)):
session_start();
echo "session start <br />";
echo "SESSION:";
var_dump($_SESSION);
endif;
require_once "Mail.php";
require_once('Mail/mime.php');


class newMail{
	protected $from;
	protected $to;
	protected $subject;
	protected $header;
	protected $smtpParas;
	protected $body;
	protected $content;
	protected $files;
	protected $template;
	public function __construct(){
		$this->files = array();
	}
	public function setFrom($from){
		$this->from = $from;
	}
	public function setTo($to){
		if ($this->to):
		$this->to .= ",".$to;
		else:
		$this->to = $to;
		endif;
	}
	public function setSubject($subject){
		$this->subject = "=?UTF-8?B?".base64_encode($subject)."?=";
	}
	public function setSmtp($username,$password,$host="smtp.126.com",$port ="25",$auth=true){
		$this->smtpParas =  array (
			  'host' => $host,
			  'port' =>$port,
			  'auth' => $auth,
			  'username' => $username,
			  'password' => $password
			 );
	}
	public function setHeaders(){
		$targets = explode(";",$this->to);
		//$to = $targets[0];
		$this->header=	array(
			 'From' =>$this->from,
			 'To' => $this->to,
			 'Subject' =>$this->subject
		 );
		
	}
	public function configEmailTemplate(){
		$css = "background:purple;color:white;padding:30px;";
		return $css;
	}
	public function setContent($content,$template=""){
		$content ="<div style='$template'>".$content."</div>";
		$this->content = $content;
		$this->template = $template;
	}
	public function addAttachment($file,$fileType="image/jpg"){
		$key = count($this->files);
		if ($file):
			$this->files[$key]['parth'] = $file;
			$this->files[$key]['type'] = $fileType;
		endif;
		return $this->files;
	}
	public function getAttachmentFiles(){
		return $this->files;	
	}
	public function setBody(){
		$content = $this->content;
		$template = $this->template;
		$files = $this->files;
		$mime = new Mail_mime();
		$html ="<html><body style='".$template."'>".$content."</body></html>";
		$mime->setHTMLBody($html);
		if (count($files)):
			foreach ($files as $file):
			 $mime->addAttachment($file['parth'],$file['type']);
			endforeach;
		endif;
		
		$param["html_charset"] ='utf-8'; //内容乱码
		$this->body = $mime->get($param);
		$this->setHeaders();
		$this->header = $mime->headers($this->header);
	}
	public function sendMail(){
		$this->setBody();
		$smtp = Mail::factory('smtp',$this->smtpParas);
		//$targets = explode(";",$this->to);
		//$targets="houhuiliang@126.com,781310958@qq.com";
		$mail = $smtp->send($this->to, $this->header, $this->body);
		return $mail;
	}
	
}
$mail = new newMail();

$mail->setFrom("houhuiliang@126.com");
$mail->setTo("781310958@qq.com");

$mail->setSubject("测试邮件类");
$mail->setSmtp("houhuiliang@126.com","18justdoit");
$template = $mail->configEmailTemplate();

$content="email test <br />sss<strong>test</strong>";

$mail->addAttachment('D:\wamp\www\testpic.png');
$mail->setContent($content,$template);
//var_dump($mail->getAttachmentFiles());
$send = $mail->sendMail();
if (isset($send)&&$send===true){
	echo "<br>successful send.<br>";
}else {
	echo "<br>failed send.<br>";
}
if (isset($send)):
$_SESSION["sendResult"] = $send;
endif;
if (isset($_SESSION)):
echo "<br />session end<br />SESSION:<br>";
var_dump($_SESSION);
endif;
//asdfsf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/*********************************************************
bysoft smtp
smtp server :bisrvmail1.bysoft.fr
port:25;
auth:false;

**********************************************************/




?> 
